package desktop.pet.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DesktopPetApp extends Application {

    private double offsetX;
    private double offsetY;
    private double scale = 1.0; // 初始缩放比例

    @Override
    public void start(Stage stage) {
        // 设置一个前置的条件，
        //00-1获得当前的系统时间
        LocalTime now = LocalTime.now();  // 获取当前时间（时分秒）
        System.out.println("当前时间: " + now);
        // 如果你想要格式化输出，例如 "14:35:20"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String formattedTime = now.format(formatter);
        System.out.println("格式化后时间: " + formattedTime);
        // 获得时分秒的三个值
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        System.out.println("时分秒: " + hour + ":" + minute + ":" + second);

        // 1. 加载宠物动态图
        Image petImage = new Image(getClass().getResource("/images/demo1.gif").toExternalForm());
        ImageView imageView = new ImageView(petImage);

        // 设置初始尺寸（可省略）
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(petImage.getWidth());
        imageView.setFitHeight(petImage.getHeight());

        // 2. 创建根布局
        StackPane root = new StackPane(imageView);
        root.setStyle("-fx-background-color: transparent;");

        // 3. 创建透明场景
        Scene scene = new Scene(root, petImage.getWidth(), petImage.getHeight());
        scene.setFill(null);

        // 4. 设置窗口无边框、透明、置顶
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setAlwaysOnTop(true);
        stage.setScene(scene);
        stage.show();

        // 5. 拖动功能
        imageView.setOnMousePressed(event -> {
            offsetX = event.getSceneX();
            offsetY = event.getSceneY();
        });

        imageView.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() - offsetX);
            stage.setY(event.getScreenY() - offsetY);
        });

        // 6. 右键菜单
        ContextMenu contextMenu = new ContextMenu();
        MenuItem settingsItem = new MenuItem("设置");
        MenuItem exitItem = new MenuItem("退出");

        exitItem.setOnAction(e -> System.exit(0));
        // 设置菜单点击后弹出设置窗口
        settingsItem.setOnAction(e -> openSettingsWindow());


        contextMenu.getItems().addAll(settingsItem, exitItem);

        imageView.setOnMouseClicked(event -> {
            if (event.getButton() == MouseButton.SECONDARY) {
                contextMenu.show(imageView, event.getScreenX(), event.getScreenY());
            } else {
                contextMenu.hide();
            }
        });

        // 7. 添加鼠标滚轮缩放事件
        scene.addEventFilter(ScrollEvent.SCROLL, event -> {
            double deltaY = event.getDeltaY();
            double scaleFactor = (deltaY > 0) ? 1.1 : 0.9;

            scale *= scaleFactor;
            scale = Math.max(0.3, Math.min(scale, 3.0)); // 限制缩放比例

            imageView.setFitWidth(petImage.getWidth() * scale);
            imageView.setFitHeight(petImage.getHeight() * scale);

            // 更新窗口大小匹配新尺寸
            stage.setWidth(imageView.getFitWidth());
            stage.setHeight(imageView.getFitHeight());

            event.consume();
        });
    }

    // 打开空白设置窗口
    private void openSettingsWindow() {
        Stage settingsStage = new Stage();
        settingsStage.initModality(Modality.APPLICATION_MODAL); // 模态窗口，阻塞主窗口
        settingsStage.setTitle("设置");

        VBox layout = new VBox(); // 空白内容
        layout.setPrefSize(300, 200); // 设置初始大小
        Scene scene = new Scene(layout);
        settingsStage.setScene(scene);

        // 显式设置关闭行为（可选）
        settingsStage.setOnCloseRequest(event -> {
            // 在关闭设置窗口前可以加一些逻辑，比如保存配置等
            System.out.println("设置窗口已关闭，主窗口解锁");
        });

        settingsStage.showAndWait(); // 阻塞式显示窗口，关闭后自动取消阻塞
    }

    public static void main(String[] args) {
        launch(args);
    }
}
